<?php
/**
 * Description: 检测指定软件的版本
 * Author: hezhizheng
 * Date: 2018/5/29
 * Time: 11:44
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Common;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Common\VersionUpdateRequest;
use App\Models\Version\Version;
use Illuminate\Http\UploadedFile;

require_once app_path('Libs/ApkParser.php');

class VersionController extends ApiController
{
    /**
     * @param VersionUpdateRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \App\Exceptions\ApiException
     */
    public function update(VersionUpdateRequest $request)
    {
        /** @var Version $version */
        $version = Version::whereType($request->type)->first();
        $this->checkItemExist($version, trans('admin.version'));

        /** @var UploadedFile|string $file */
        $file = $request->software;

        if (is_file($file)) {
            $this->checkIsTrue(in_array($file->getClientOriginalExtension(), ['apk']), trans('admin.has_apk'));

            $apkInfo = $this->getApkInfo($file);

            $update_data = [
                'software_url' => storage_path($file->getClientOriginalName()),
                'version'      => $apkInfo->getVersionName(),
                'remark'       => $request->remark,
            ];
        } else {
            
            $header_array = get_headers($file, true); // 直接获取头信息,file_get_contents 会读取整个文件
            $file_size = $header_array['Content-Length'];

            $update_data = [
                'software_url' => $request->software, // 实际路径
                'version'      => $request->version,
                'remark'       => $request->remark,
                'size'         => $file_size
            ];

//            dd($update_data);
        }

        $update = $version->update($update_data);

        if (is_file($file)) @unlink(storage_path($file->getClientOriginalName()));

        return $this->response(compact('update'));
    }

    /**
     * @param $type
     *
     * @return array|\Illuminate\Http\Response
     * @throws \App\Exceptions\ApiException
     */
    public function show($type)
    {
        $version = Version::whereType($type)->first();

        $this->checkItemExist($version, trans('admin.version'));

        return $this->response(compact('version'));
    }


    /**
     * @param UploadedFile $file
     * @return \Apkparser
     */
    private function getApkInfo(UploadedFile $file)
    {
        $file_name = $file->getClientOriginalName();

        $file->move(storage_path(), $file_name);
        /**
         * @link http://www.jb51.net/article/53780.htm
         * @var $appObj
         */
        $appObj = new \Apkparser();

        $targetFile = storage_path($file_name); //apk所在的路径地址

//        dd($targetFile);
        $appObj->open($targetFile);

//        $appObj->getAppName();     // 应用名称
//        $appObj->getPackage();    // 应用包名
//        $appObj->getVersionName();  // 版本名称
//        $appObj->getVersionCode();  // 版本代码
//        $version_name = $appObj->getVersionName();  // 版本名称
//        dd($appObj->getAppName(), $appObj->getPackage(), $appObj->getVersionCode(), $version_name);

        return $appObj;


    }
}